/*

    particleswarm.h - definition of the particleswarm class
    Author Julio Agustin Barrera Mendoza
    Copyright (C) 2009 CINVESTAV-IPN.

    This program is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License as published by 
    the Free Software Foundation; either version 2 of the License, 
    or (at your option) any later version.

    This program is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
    General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to 
    the Free Software Foundation, Inc., 59 Temple Place, 
    Suite 330, Boston, MA 02111-1307 USA

*/
 
#ifndef PARTICLESWARM_H
#define PARTICLESWARM_H

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include "particle.h"
#include "base/range.h"
#include "base/evaluationfunction.h"
#include "base/functions.h"
#include "random/random.h"
#include "utils/utils.h"

using namespace std;

// ------------------------------------------------------------------
// CLASS ParticleSwarm
// ------------------------------------------------------------------

class ParticleSwarm
{
  public:
    ParticleSwarm();
    virtual ~ParticleSwarm();
  
    unsigned int &Particles(void) {return particles;}
    const unsigned int &Particles(void) const {return particles;}
    
    unsigned int &Iterations(void) {return iterations;}
    const unsigned int &Iterations(void) const {return iterations;}

    virtual void loadOptions(const string &filename);
    virtual void printOptions(void);

    virtual void generateSwarm(void);
    void printSwarm(void);
    void printParticle(unsigned int index);

    virtual void updateVelocity(void);
    virtual void updatePosition(void);
    void updatePersonalBest(void);
    
    void evaluateParticles(void);

    unsigned int findGlobalBest(void);

    virtual void clipVelocity(void);
    virtual void clipPosition(void);
    
  protected:
    unsigned int iterations;
    unsigned int particles;
    long double c1, c2;
    string functionName;
    Random random;
    vector<Particle> swarm;
    vector<Range> ranges;
    EvaluationFunction function;
};

#endif
